<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author yangweijie <yangweijiester@gmail.com>
 */
class OrderController extends AdminController {


    public function order(){
        $param = $_GET;
        $start_time = $_GET['start_time'];
        $end_time = $_GET['end_time'];
        $this->assign("start_time",$start_time);
        $this->assign("end_time",$end_time);
        if($_GET['status'] == ""){
            $param['status'] = "2";
        }
        $this->assign("status",$param['status']);
        // 构造查询条件
        $where = array();
        if($start_time){
            $where['create_time'] = array("between",array(strtotime($start_time),strtotime($end_time)));
        }
        if($param['status']){
            $where['status'] = $param['status'];
        }

        $param['type']?$where['type']=$param['type']:"";
        $this->assign("type",$param['type']);
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        if($uid != '1'){
            $selectsupplier = $uid;
        }

        $list = $this->lists('Invest', $where,"",true);
        $return = array();
        foreach($list as $key=>$val){
            $good_info = json_decode($val['good_info'],true);
            if(!is_array($good_info)){
                $good_info = json_decode($good_info,true);
            }
            $receipt_id = $val['receipt_id'];
            $receiptinfo = M("Receipt")->where(array("id"=>$receipt_id))->find();
            $val['receiptinfo'] = $receiptinfo;
            unset($val['good_info']);
            $val['amount'] = floatval($val['amount']);
            $val['status'] = intval($val['status']);
            $val['type'] = intval($val['type']);
            if($val['type'] == 1){
                $val['type_name'] = "抢购商品";
            }elseif($val['type']==2){
                $val['type_name'] = "预约商品";
            }elseif($val['type']==3){
                $val['type_name'] = "定制商品";
            }
            if($val['status'] == 1){
                $val['status_name'] = "完成";
            }elseif($val['status']==3){
                $val['status_name'] = "待发货";
            }elseif($val['status']==4){
                $val['status_name'] = "待收货";
                $expressinfo = M("Express")->where(array("invest_id"=>$val['id']))->find();
                switch($expressinfo['type']){
                    case 1:
                        $val['posttype'] = "顺丰";
                        break;
                    case 2:
                        $val['posttype'] = "深通";
                        break;
                    case 3:
                        $val['posttype'] = "圆通";
                        break;
                    case 4:
                        $val['posttype'] = "韵达";
                        break;
                    case 5:
                        $val['posttype'] = "大丰";
                        break;
                    case 6:
                        $val['posttype'] = "诚信";
                        break;
                }
                $val['postid'] = $expressinfo['express_code'];
            }elseif($val['status']==5){
                $val['status_name'] = "待评价";
            }elseif($val['status']==2){
                $val['status_name'] = "待付款";
            }elseif($val['status']==10){
                $val['status_name'] = "已取消";
            }
            $val['create_time'] = date("Y-m-d H:i:s",$val['create_time']);

            if($val['status'] == '3'){
                $val['controller'] = '<a class="open-html open-express"  code="'.$val['id'].'">发货</a><br>';
            }
            if($val['status'] == '1'){
                $val['controller'] = '<a class="open-html open-delivery" href="admin.php?s=/Order/checkevaluated/id/'.$val['id'].'.html" >查看评价</a><br>';
            }
            if($val['status'] == '2'){
                $val['controller'] = '<a class="cacelorder" code="'.$val['id'].'">取消</a><br>';
                $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
                if($uid == '1'){
                    $val['controller'] .= '<a class="changeamount" code="'.$val['id'].'">修改价格</a><br>';
                }
            }
            $return[$key]=$val;
            $good_info = M("InvestGoods")->where(array("invest_id"=>$val['id']))->select();
            foreach($good_info as $k=>$v){
                $goodlist = M("Good")->where(array("id"=>$v['goods_id']))->find();
                $return[$key]["goodlist"][$k]['img_path'] = "http://114.215.209.229/".explode(",",$goodlist['img_path'])[0];
                $return[$key]["goodlist"][$k]["good_name"] =  $goodlist['title'];
                $return[$key]["goodlist"][$k]["count"] =  intval($v['count']);
                $return[$key]["goodlist"][$k]["min_price"] =  floatval($v['price']);
            }
            
        }
        int_to_string($return);
        $this->assign('_list', $return);
        $this->display();
    }
    public function changeexpress(){
        $id = $_POST['id'];
        $express_code = $_POST['express_code'];
        $express_type = $_POST['express_type'];
        M("Invest")->where(array("id"=>$id))->save(array("status"=>'4'));
        M("Express")->add(array("express_code"=>$express_code,"type"=>$express_type,"invest_id"=>$id));
    }
    //取消订单
    public function cancelorder(){
        $id = $_POST['id'];
        M("Invest")->where(array("id"=>$id))->save(array("status"=>'10'));
    }
    //评论列表
    public function checkevaluated(){
        $id = $_GET['id'];
        $commentinfo =  M("Comment")
            ->join("left join mt_good on mt_comment.good_id=mt_good.id")->where(array("mt_comment.invest_id"=>$id))->select();
        $this->assign("_list",$commentinfo);
        $this->display();
    }
    //取消订单
    public function changeamount(){
        $id = $_POST['id'];
        $amount = $_POST['amount'];
        M("Invest")->where(array("id"=>$id))->save(array("amount"=>$amount));
    }
    //导出数据导excel
    public function downloadexcel(){
        $start_time = $_GET['start_time'];
        $end_time = $_GET['end_time'];
        $selectstatus = $_GET['status'];
        $uid = $_SESSION['onethink_admin']['user_auth']['uid'];
        $_GET['type']?$where['type']=$_GET['type']:"";
        if($uid != '1'){
            $selectsupplier = $uid;
        }
        if($selectsupplier){

            $goodlist = M("Good")->where(array("supplier"=>$selectsupplier))->select();
            $goodsql = "";
            foreach($goodlist as $key=>$val){
                $gooddata[$val['id']]['supplier'] = $val['supplier'];
                $gooddata[$val['id']]['good_name'] = $val['title'];
                //获取该供应商下面的商品
                $goodsql .= 'good_info like \'%"good_id":"'.$val['id'].'"%\' or good_info like \'%"good_id\":\"'.$val['id'].'\"%\' or';
            }
            $goodsql = substr($goodsql,0,-2);
            $where["_string"] = $goodsql;
        }
        //
        if($selectstatus){
            $where["status"] = $selectstatus;
        }
        if($start_time){
            $where['create_time'] = array("between",array(strtotime($start_time),strtotime($end_time)));
        }
            $list = M("Invest")->where($where)->order("create_time desc")->select();
            $returninfo = array();
            foreach ($list as $key => $val) {
                $good_info = json_decode($val['good_info'], true);
                if (!is_array($good_info)) {
                    $good_info = json_decode($good_info, true);
                }
                $goodid = $good_info[0]['good_id'];
                $val['good_name'] = $gooddata[$goodid]['good_name'];
                $val['count'] = $good_info[0]['count'];
                if($val['type'] == 1){
                    $val['type_name'] = "抢购商品";
                }elseif($val['type']==2){
                    $val['type_name'] = "预约商品";
                }elseif($val['type']==3){
                    $val['type_name'] = "定制商品";
                }
                if ($val['status'] == 1) {
                    $val['status_name'] = "完成";
                } elseif ($val['status'] == 3) {
                    $val['status_name'] = "待发货";
                } elseif ($val['status'] == 4) {
                    $val['status_name'] = "待收货";
                } elseif ($val['status'] == 5) {
                    $val['status_name'] = "待评价";
                } elseif ($val['status'] == 2) {
                    $val['status_name'] = "待付款";
                } elseif ($val['status'] == 10) {
                    $val['status_name'] = "已取消";
                }
                $receipt_id = $val['receipt_id'];
                $val['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
                $receiptinfo = M("Receipt")->where(array("id"=>$receipt_id))->find();
                $val['receiptinfo'] = $receiptinfo;

                foreach($good_info as $k=>$v){
                    $val["sku_info"] = "";
                    if($v['sku'] == ""){
                        $goodlist = M("Good")->where(array("id"=>$v['good_id']))->find();
                    }else{
                        $goodlist = M("Good")
                            ->join("left join mt_good_price on mt_good.id=mt_good_price.good_id")
                            ->where(array("mt_good.id"=>$v['good_id'],"mt_good_price.id"=>$v['sku']))->find();
                        $card_ids = explode(",",$goodlist['category_id']);
                        $where = array();
                        $where['id'] = array("in",$card_ids);
                        $cateinfo = M("GoodCategory")->where($where)->select();
                        $catelist = "";
                        if(is_array($cateinfo)){
                            foreach($cateinfo as $ks=>$vs){
                                $catelist .= $vs['name'].":".$vs['value'].",";
                            }
                        }

                        $val["sku_info"] =  count($cateinfo)>0?substr($catelist,0,-1):"";
                    }
                    $val["good_name"] =  $goodlist['title'];
                    $val["count"] =  intval($v['count']);
                    $val["min_price"] =  floatval($v['price']);
                }
                $returninfo[$key] = $val;
        }
        $date = date("Y-m-d");
        header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Transfer-Encoding: binary ");
        header("Content-Disposition:attachment;filename=order_$date.xls" );
        echo iconv("utf-8", "gb2312", "订单编号")."\t";
        echo iconv("utf-8", "gb2312", "订单类型")."\t";
        echo iconv("utf-8", "gb2312", "商品名称")."\t";
        echo iconv("utf-8", "gb2312", "商品属性")."\t";
        echo iconv("utf-8", "gb2312", "商品编码")."\t";
        echo iconv("utf-8", "gb2312", "商品数量")."\t";
        echo iconv("utf-8", "gb2312", "实付款")."\t";
        echo iconv("utf-8", "gb2312", "用户id")."\t";
        echo iconv("utf-8", "gb2312", "收货信息")."\t";
        echo iconv("utf-8", "gb2312", "操作时间")."\t";
        echo iconv("utf-8", "gb2312", "订单状态")."\t";
        echo "\n";
        foreach($returninfo as $key=>$val){
            echo iconv("utf-8", "gb2312",$val['order_no'])."\t";
            echo iconv("utf-8", "gb2312",$val['type_name'])."\t";
            echo iconv("utf-8", "gb2312",$val['good_name'])."\t";
            $encode = mb_detect_encoding($val['sku_info'], array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
            if($encode == "UTF-8"){
                echo iconv("utf-8", "gbk",$val['sku_info'])."\t";
            }else{
                echo "\t";
            }
            echo iconv("utf-8", "gb2312",$val['code'])."\t";
            echo iconv("utf-8", "gb2312",$val['count'])."\t";
            echo iconv("utf-8", "gb2312",$val['amount'])."\t";
            echo iconv("utf-8", "gb2312",$val['user_id'])."\t";
            $r = $val['receiptinfo']['province'].$val['receiptinfo']['city'].$val['receiptinfo']['area'].
                $val['receiptinfo']['address']." 联系人".$val['receiptinfo']['username']." 联系人手机".$val['receiptinfo']['phone'];
            echo iconv("utf-8", "gbk",$r)."\t";
            echo iconv("utf-8", "gb2312",$val['create_time'])."\t";
            echo iconv("utf-8", "gb2312",$val['status_name'])."\t";
            echo "\n";
        }
    }
    public function ordinary()
    {
        $order_number = $_GET['order_number'];
        $phone_number = $_GET["phone_number"];
        $order_time = $_GET["order_time"];
        $user_id = $_GET["user_id"];
        if ($phone_number) {
            $where['phone_number'] = $phone_number;
        }
        $order_time = $_GET["order_time"];
        if ($order_time) {
            $where['order_time'] = $order_time;
        }
        $user_id = $_GET["user_id"];
        if ($user_id) {
            $where['user_id'] = $user_id;
        }
        $list = $this->lists('Order_ordinary', $where, "", true);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->display();
    }






    public function group()
    {
        $where = array();
        $order_number = $_GET["order_number"];
        if ($order_number) {
            $where['order_number'] = $order_number;
        }
        $phone_number = $_GET["phone_number"];
        if ($phone_number) {
            $where['phone_number'] = $phone_number;
        }
        $order_time = $_GET["order_time"];
        if ($order_time) {
            $where['order_time'] = $order_time;
        }
        $user_id = $_GET["user_id"];
        if ($user_id) {
            $where['user_id'] = $user_id;
        }
        $list = $this->lists('GroupOrder', $where, "", true);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->display();
    }

//*******************************************************************
    public function addOrder_ordinary()
    {
        $data = array();
        $data['order_number'] = $_POST['order_number'];
        $data['phone_number'] = $_POST['phone_number'];
        $data['order_time'] = $_POST['order_time'];
        $data['user_id'] = $_POST['user_id'];
        $data['all'] = $_POST['all'];
        $data['pending_payment'] = $_POST['pending_payment'];
        $data['to_be_shipped'] = $_POST['to_be_shipped'];
        $data['already_shipped'] = $_POST['already_shipped'];
        $data['evaluated'] = $_POST['evaluated'];
        $data['completed'] = $_POST['completed'];
        $data['return'] = $_POST['return'];
        $data['canceled'] = $_POST['canceled'];
        $data['information'] = $_POST['information'];
        $data['code'] = $_POST['code'];
        $data['number'] = $_POST['number'];
        $data['payment'] = $_POST['payment'];
        $data['receiving_information'] = $_POST['receiving_information'];
        $data['state'] = $_POST['state'];
        $data['operation'] = $_POST['operation'];
        $data['group_id'] = $_POST['group_id'];
        $data['group_type'] = $_POST['group_type'];
        $data['group'] = $_POST['group'];
        $data['group_failure'] = $_POST['group_failure'];


        if($_POST['title']>0){

            M("Ative")->where(array("title"=>$_POST['title']))->save($data);
            $goodid = $_POST['title'];
        }else {
            $goodid = M("title")->add($data);
        }





    }


    }
